package top.xana.acg.anime.ui.frag

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import top.xana.acg.anime.R
import top.xana.acg.anime.databinding.SearchContentFragmentBinding
import top.xana.acg.anime.model.Anime
import top.xana.acg.common.base.adp.RecyclerAdapter
import top.xana.acg.common.base.ui.BaseFragment

class SearchContentFragment : BaseFragment() {

    companion object {
        fun newInstance() = SearchContentFragment()
        const val TAG = "SearchContentFragment"
    }

    override fun getLayoutId() = R.layout.search_content_fragment
    val b by lazy {
        getBinding<SearchContentFragmentBinding>()
    }

    override fun initView() {
        super.initView()

        val arr = resources.getStringArray(R.array.searchTab)

        b.viewPager2.adapter = ViewPagerAdapter(
            arr.mapIndexed { i, s ->
                val itemView =
                    if (i == 0) R.layout.vlayout_hot_view
                    else R.layout.item_search_malimali
                SearchListFragment.newInstance(itemView)
            },
            childFragmentManager,
            lifecycle
        )
        TabLayoutMediator(b.tabLayout, b.viewPager2) { tab, i ->
            tab.text = arr[i]

        }.attach()
    }

    class ViewPagerAdapter(val frags: List<Fragment>, fm: FragmentManager, lc: Lifecycle) :
        FragmentStateAdapter(fm, lc) {
        override fun getItemCount() = frags.size

        override fun createFragment(i: Int) = frags[i]

    }


}